window.onload = function () {
    axios.post("http://localhost:8081/user/common").then(res => {
        var username = document.getElementById("username");
        if(res.data.state){
            var admin = res.data.admin;
            username.innerText = admin.username;
        }
    })
    var show = new Vue({
        el: "#select",
        data: {
            lists:[{

            }]
        },
        methods: {
            showAllUser:function (){
                axios.post("http://localhost:8081/admin/getAllUser").then(res => {
                    if(res.data.state) {
                        this.lists = res.data.users;
                        for (var i = 0; i < this.lists.length; i++) {
                            switch (this.lists[i].type) {
                                case '0':
                                    this.lists[i].type = "剪发";
                                    break;
                                case '1':
                                    this.lists[i].type = "洗发";
                                    break;
                                case '2':
                                    this.lists[i].type = "护理";
                                    break;
                                case '3':
                                    this.lists[i].type = "补发根";
                                    break;
                                case '4':
                                    this.lists[i].type = "染发";
                                    break;
                                case '5':
                                    this.lists[i].type = "黑油";
                                    break;
                                case '6':
                                    this.lists[i].type = "烫发+剪发";
                                    break;
                                case '7':
                                    this.lists[i].type = "烫发+染发";
                                    break;
                                case '8':
                                    this.lists[i].type = "洗发+染发";
                                    break;
                                case '9':
                                    this.lists[i].type = "剪发+黑油";
                                    break;
                                case '10':
                                    this.lists[i].type = "剪发+染发";
                                    break;
                            }
                        }
                    }
                })
            }
        }
    });
    show.showAllUser();
}
function logout() {
    var flag = confirm("确定退出？");
    if(flag){
        axios.post("http://localhost:8081/admin/logout").then(res => {
            if(res.data.state){
                location.href = "../html/login.html";
            }
        })
    }
}

// function reduce(){
//   减少按钮
// }
function deleteCurrentRow(obj,tellphone) {
    axios.post("http://localhost:8081/admin/deleteUser",{tellphone:tellphone}).then(res => {
        alert(res.data.msg);
        if(res.data.state){
            var tr = obj.parentNode.parentNode;
            var tbody = tr.parentNode;
            tbody.removeChild(tr);
            console.log(obj)
            //只剩行首时删除表格
        }
    })
}

function mreduce(obj,times,tellphone) {
    if(times === '0'){
        alert("当前次数不足，请充值！");
        return false;
    }
    var kind = obj;
    var flag = confirm("确定减少"+kind+"次数？");
    if(flag){
        axios.post("http://localhost:8081/admin/timesReduce",{type:kind,tellphone:tellphone}).then(res => {
          if(res.data.state){
              alert(kind+"次数-1");
              location.href = "../html/MembershipManagement.html";
          }else{
              alert(type+"操作失败！");
          }
        })
    }


}

function searchUser(){
    setTimeout(function(){//因为是即时查询，需要用setTimeout进行延迟，让值写入到input内，再读取
        var storeId = document.getElementById('select');//获取table的id标识
        var selectTel = document.getElementById('selectTel');//获取table的id标识
        var rowsLength = storeId.rows.length;//表格总共有多少行
        var key = selectTel.value;//获取输入框的值
        var searchCol = 2;//要搜索的哪一列，这里是第一列，从0开始数起
        var seleCol = 1;
        if(!isNaN(key)){
            for(var i=1;i<rowsLength;i++){//按表的行数进行循环，本例第一行是标题，所以i=1，从第二行开始筛选（从0数起）
                var searchText = storeId.rows[i].cells[searchCol].innerHTML;//取得table行，列的值
                if(searchText.match(key)){//用match函数进行筛选，如果input的值，即变量 key的值为空，返回的是ture，
                    storeId.rows[i].style.display='';//显示行操作，
                }else{
                    storeId.rows[i].style.display='none';//隐藏行操作
                }
            }
        }
        else{
            for(var q=1;q<rowsLength;q++){//按表的行数进行循环，本例第一行是标题，所以i=1，从第二行开始筛选（从0数起）
                var searchVal = storeId.rows[q].cells[seleCol].innerHTML;//取得table行，列的值
                if(searchVal.match(key)){//用match函数进行筛选，如果input的值，即变量 key的值为空，返回的是ture，
                    storeId.rows[q].style.display='';//显示行操作，
                }else{
                    storeId.rows[q].style.display='none';//隐藏行操作
                }
            }
        }
    },200);//200为延时时间
}
